﻿#region Using Statements
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System;
using System.Collections.Generic;
using RhinoXNA.RhinoUtilities;
#endregion

namespace RhinoXNA.RhinoGraphics.Rhino2D
{
    public abstract class SpriteEntity : IRhinoDrawable
    {
        public Rectangle SourceRect { get; set; }

        public Rectangle BoundingBox
        {
            get
            {
                return new Rectangle((int)(this.Position.X - (this.SourceRect.Width / 2)),
                    (int)(this.Position.Y - (this.SourceRect.Height / 2)),
                    (int)(this.SourceRect.Width),
                    (int)(this.SourceRect.Height));
            }
        }

        private Texture2D _tex;

        public Texture2D Texture
        {
            get { return _tex; }
        }

        public SpriteEntity(RhinoXNAAdapter adapter, List<String> parameters)
            : base (adapter.SpriteBatch)
        {
            bool vis = false;
            if (parameters[9].ToLower() == "true")
                vis = true;

            _tex = adapter.ContentManager.Load<Texture2D>(parameters[1]);
            this.Position = RhinoLoader.GetPosition(adapter, _tex, parameters[3], parameters[4]);
            this.Rotation = MathHelper.ToRadians(Convert.ToInt32(parameters[5]));
            this.Scale = new Vector2(float.Parse(parameters[6]), float.Parse(parameters[7]));
            this.Layer = float.Parse(parameters[8]);
            this.SourceRect = new Rectangle(Convert.ToInt32(parameters[10]), Convert.ToInt32(parameters[11]),
                    Convert.ToInt32(RhinoLoader.GetBoundingVolume(_tex, parameters[12], parameters[13]).X),
                    Convert.ToInt32(RhinoLoader.GetBoundingVolume(_tex, parameters[12], parameters[13]).Y));
            this.isVisible = vis;
            this.Handle = parameters[2];
        }

        public SpriteEntity(SpriteBatch sBatch, Texture2D texture, String handle, Rectangle src, Vector2 pos, float rot, Vector2 scale, bool visible, float layer)
            : base(sBatch)
        {
            _tex = texture;
            this.Position = pos;
            this.Rotation = rot;
            this.Scale = scale;
            this.Layer = layer;
            this.SourceRect = src;
            this.isVisible = visible;
            this.Handle = handle;
        }

        public override void Initialize(string fileToLoad)
        {
            
        }

        protected abstract void Animate(GameTime gameTime);

        public override void Update(GameTime gameTime)
        {
            Animate(gameTime);
        }

        public override void Render()
        {
            if (this.isVisible)
            {
                _sBatch.Begin();
                _sBatch.Draw(_tex, this.Position, this.SourceRect,
                    Color.White, this.Rotation, new Vector2(this.SourceRect.Width / 2, this.SourceRect.Height / 2), this.Scale,
                    SpriteEffects.None, this.Layer);
                _sBatch.End();
            }
        }
    }
}
